package godau.fynn.dsbdirect.table.reader;

import android.content.Context;
import godau.fynn.dsbdirect.Utility;
import godau.fynn.dsbdirect.table.Entry;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class WhiteYellow extends FlexibleReader {

    private static final int UNDEFINED = 0;
    private static final int CLASS = 1;
    private static final int SUBJECT = 2;
    private static final int LESSON = 3;
    private static final int TEACHER = 5;
    private static final int ROOM = 6;
    private static final int INFO = 10;
    private static final int MASTER_SIZE = 12;

    public WhiteYellow(String html, Context context) {
        super(html, context);
    }

    public ArrayList<Entry> read() {
        Document d = Jsoup.parse(super.mHtml);

        // Get date
        String titleString = d.getElementsByClass("TextUeberschrift").first().text();
        // "Vertretungsplan für Tag, DD.MM.YYYY (WocheX)
        titleString = titleString.split(" ")[3];

        String[] dateDigits = titleString.split(" ")[0].split("\\.");

        Calendar calendar = Utility.zeroOClock(Calendar.getInstance());
        calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateDigits[0]));
        calendar.set(Calendar.MONTH, Integer.parseInt(dateDigits[1]) - 1);
        calendar.set(Calendar.YEAR, Integer.parseInt(dateDigits[2]));
        Date date = calendar.getTime();

        Element tabelleVertretungen = d.getElementsByClass("TabelleVertretungen").first();

        // Get definitions

        Element titelZeile = tabelleVertretungen.getElementsByClass("TitelZeileTabelleVertretungen").first();

        String[] mainTableDefinition =
                stringsOfElements(
                        titelZeile.getElementsByTag("td")
                );

        int[] positions = getMasterTablePositions(mainTableDefinition);

        // Get data from table

        Elements trs = tabelleVertretungen.getElementsByTag("tr");

        // Skip first row as it contains the definitions
        for (int i = 1; i < trs.size(); i++) {
            Element tr = trs.get(i);

            String[] masterRow = new String[MASTER_SIZE];
            Elements tds = tr.getElementsByTag("td");

            for (int j = 0; j < tds.size(); j++) {
                masterRow[positions[j]] = tds.get(j).text();
            }

            constructEntry(masterRow, date);
        }

        return super.getEntries();
    }

    @Nullable
    @Override
    public String getSchoolName() {
        return null;
    }

    protected int getMasterTablePosition(String definition) {

        switch (definition) {
            case "Klasse":
                return CLASS;
            case "Std":
                return LESSON;
            case "Fach":
                return SUBJECT;
            case "Lehrer":
                return TEACHER;
            case "Raum":
                return ROOM;
            case "Sonstiges":
                return INFO;
            default:
                return UNDEFINED;
        }
    }

}
